<?php
// +----------------------------------------------------------------------
// | 后台用户管理
// +----------------------------------------------------------------------
namespace app\admin\controller\v1;
use app\admin\Adminbase;

class User extends Adminbase 
{
	
	/**
	* 审核代理商
	* /admin/v1/User/approvalApplyAgent
	* 需要登录 
	* @is_need_login true
	*/
	public function approvalApplyAgent($ids){
		if(empty($ids)){
			return $this->ReError("参数错误！");
		}
		try {
			$result = model('UserApplyAgent')->approval($ids);
			if(!$result){
				return $this->ReError($result);
			}
			return $this->ReSucess(true);	
		} catch (\Exception $e) {
			return $this->ReError($e->getMessage());	
		}
	}
	
	/**
	* 删除代理商
	* /admin/v1/User/delAgent
	* 需要登录 
	* @is_need_login true
	*/
	public function delAgent($ids){
		if(empty($ids)){
			return $this->ReError("参数错误！");
		}
		try {
			$result = model('UserApplyAgent')->approval($ids);
			if(!$result){
				return $this->ReError(model('UserApplyAgent')->errorInfo);
			}
			return $this->ReSucess(true);	
		} catch (\Exception $e) {
			return $this->ReError($e->getMessage());	
		}
	}


	/**
	* 提现驳回
	* /admin/v1/User/withdrawReject
	* 需要登录 
	* @is_need_login true
	*/
	public function withdrawReject($id,$desc){
		if(empty($id) || empty($desc)){
			return $this->ReError("参数错误！");
		}
		try {
			$result = model('UserExtract')->reject($id,$desc);
			if(!$result){
				return $this->ReError(model('UserExtract')->errorInfo);
			}
			return $this->ReSucess(true);	
		} catch (\Exception $e) {
			return $this->ReError($e->getMessage());	
		}
	}
	
	
	/**
	* 提现确认
	* /admin/v1/User/withdrawConfirm
	* 需要登录 
	* @is_need_login true
	*/
	public function withdrawConfirm($id){
		if(empty($id)){
			return $this->ReError("参数错误！");
		}
		try {
			$result = model('UserExtract')->confirm($id);
			if(!$result){
				return $this->ReError(model('UserExtract')->errorInfo);
			}
			return $this->ReSucess(true);	
		} catch (\Exception $e) {
			return $this->ReError($e->getMessage());	
		}
	}
	
	
	/**
	* 门店变更-审批通过
	* /admin/v1/user/approvalChangeShop
	* 需要登录 
	* @is_need_login true
	*/
	public function approvalChangeShop($id){
		try {
			model('UserChangeShop')->changeShop($id);
			return $this->ReSucess('','审核成功！');	
		} catch (\Exception $e) {
			return $this->ReError($e->getMessage());	
		}
	}
	
	/**
	* 门店变更-驳回申请
	* /admin/v1/user/rejectChangeShop
	*/
	public function rejectChangeShop(String $ids,String $desc){
		try {
			model('UserChangeShop')->reject($ids,$desc);
			return $this->ReSucess('','已驳回！');	
		} catch (\Exception $e) {
			return $this->ReError($e->getMessage());	
		}
	}
	
	
	/**
	* 会员充值-获取支付签名
	* /admin/v1/user/recharge
	* 是否需要登录 
	* @is_need_login true
	* 是否需要注册会员信息
	*/
	public function recharge($price,$uid,$type,$mark=''){
		if( !$price || !$uid){
			return $this->ReError('参数错误！');	
		}
		// 启动事务
		\Db::startTrans();
		try {
		   	$order = model("UserRecharge")->add([
				'price'=>$price,
				'uid'=>$uid,
				'type'=>$type,
				'mark'=>$mark 
			]);
			$res = model("UserRecharge")->paySuccess($order['no'],'',true);
		    // 提交事务
		    \Db::commit();
		    return $this->ReSucess();	
		} catch (\Exception $e) {
			\Db::rollback();
			return $this->ReError($e->getMessage());	
		}
	}
	
}

